package com.ruoyi.project.mahjong.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.TIntegralMapper;
import com.ruoyi.project.mahjong.domain.TIntegral;
import com.ruoyi.project.mahjong.service.ITIntegralService;

/**
 * 积分赠送Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-28
 */
@Service
public class TIntegralServiceImpl implements ITIntegralService
{
    @Autowired
    private TIntegralMapper tIntegralMapper;

    /**
     * 查询积分赠送
     *
     * @param id 积分赠送ID
     * @return 积分赠送
     */
    @Override
    public TIntegral selectTIntegralById(Long id)
    {
        return tIntegralMapper.selectTIntegralById(id);
    }

    /**
     * 查询积分赠送列表
     *
     * @param tIntegral 积分赠送
     * @return 积分赠送
     */
    @Override
    public List<TIntegral> selectTIntegralList(TIntegral tIntegral)
    {
        return tIntegralMapper.selectTIntegralList(tIntegral);
    }

    /**
     * 新增积分赠送
     *
     * @param tIntegral 积分赠送
     * @return 结果
     */
    @Override
    public int insertTIntegral(TIntegral tIntegral)
    {
        tIntegral.setCreateTime(DateUtils.getNowDate());
        return tIntegralMapper.insertTIntegral(tIntegral);
    }

    /**
     * 修改积分赠送
     *
     * @param tIntegral 积分赠送
     * @return 结果
     */
    @Override
    public int updateTIntegral(TIntegral tIntegral)
    {
        TIntegral check=selectTIntegralById(tIntegral.getId());
        tIntegral.setUpdateTime(DateUtils.getNowDate());
        if(ObjectUtil.isNotNull(check.getStatus())){
            tIntegral.setStatus(check.getStatus().equals("1")?"0":"1");
        }
        return tIntegralMapper.updateTIntegral(tIntegral);
    }

    /**
     * 批量删除积分赠送
     *
     * @param ids 需要删除的积分赠送ID
     * @return 结果
     */
    @Override
    public int deleteTIntegralByIds(Long[] ids)
    {
        return tIntegralMapper.deleteTIntegralByIds(ids);
    }

    /**
     * 删除积分赠送信息
     *
     * @param id 积分赠送ID
     * @return 结果
     */
    @Override
    public int deleteTIntegralById(Long id)
    {
        return tIntegralMapper.deleteTIntegralById(id);
    }

    @Override
    public TIntegral selectKeYongTIntegralList( ) {
        return tIntegralMapper.selectKeYongTIntegralList();
    }

    @Override
    public List<TIntegral> getIntegral(Long shopId) {
        TIntegral integral=new TIntegral();
        integral.setShopId(shopId);
        List<TIntegral> tIntegralList= tIntegralMapper.selectTIntegralList(integral);
        List<TIntegral> result=new ArrayList<>();
        TIntegral integralone=  getTIntegral("0",tIntegralList,shopId);
        TIntegral integraltwo=  getTIntegral("1",tIntegralList,shopId);
        result.add(integralone);
        result.add(integraltwo);
        return result;
    }

    @Override
    public TIntegral selectRechargeGiveintrgral(Long shopId) {
        return tIntegralMapper.selectRechargeGiveintrgral(shopId);
    }

    private TIntegral getTIntegral(String type, List<TIntegral> tIntegralList,Long shopId) {
        TIntegral tIntegral=new TIntegral();
        tIntegral.setType(type);
        tIntegral.setShopId(shopId);
        if(ObjectUtil.isNull(tIntegralList)){
            tIntegral.setStatus("0");
            insertTIntegral(tIntegral);
            return  tIntegral;
        }
        boolean containsPineapple = tIntegralList.stream()
                .anyMatch(fruit -> fruit.getType().equals(type));
        if(!containsPineapple){
            tIntegral.setStatus("0");
            insertTIntegral(tIntegral);
            return tIntegral;
        }
        for(TIntegral integral:tIntegralList){
            if(integral.getType().equals(type)){
                return integral;
            }
        }
        return null;
    }
}
